package com.xiaxiang.xiawan.My.Adapter;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.pbq.imagepicker.VideoPicker;
import com.pbq.imagepicker.bean.VideoItem;
import com.xiaxiang.xiawan.R;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import cn.drj.myutils.adapter.DRJBaseAdapter;
import cn.drj.myutils.adapter.DRJViewHolderHelper;

/**
 * Created by gz on 2016/9/11.
 */
public class LocalPhotoAdapter extends DRJBaseAdapter<VideoItem> {
    private boolean isEdit = false;
    private VideoPicker videoPicker;
    private ArrayList<VideoItem> mSelectedVideos; //全局保存的已经选中的视频数据

    public LocalPhotoAdapter(Context context) {
        super(context, R.layout.adp_item_local);
        mContext = context;
        videoPicker = VideoPicker.getInstance();
        videoPicker.setSelectLimit(100);
        mSelectedVideos = videoPicker.getSelectedVideos();
    }

    @Override
    protected void setItemChildListener(DRJViewHolderHelper viewHolderHelper) {

    }

    @Override
    public void fillData(DRJViewHolderHelper viewHolderHelper, final int position, VideoItem model) {
        final CheckBox checkBox = viewHolderHelper.getView(R.id.ckCheck);
        final VideoItem videoItem = getItem(position);
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int selectLimit = videoPicker.getSelectLimit();
                if (checkBox.isChecked() && mSelectedVideos.size() >= selectLimit) {
                    checkBox.setChecked(false);
                } else {
                    videoPicker.addSelectedVideoItem(position, videoItem, checkBox.isChecked());
                }
                Log.i("drj",mSelectedVideos.toString());
            }
        });

        viewHolderHelper.setText(R.id.name, videoPicker.timeParse(model.timeLong));

        boolean checked = mSelectedVideos.contains(videoItem);
        if (checked) {
            checkBox.setChecked(true);
        } else {
            checkBox.setChecked(false);
        }

        Glide.with(mContext).load(model.path).
                into((ImageView) viewHolderHelper.getView(R.id.icon));

        if(isEdit()){
            viewHolderHelper.getView(R.id.ckCheck).setVisibility(View.VISIBLE);
        }else {
            viewHolderHelper.getView(R.id.ckCheck).setVisibility(View.GONE);
        }
    }

    public boolean isEdit() {
        return isEdit;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    public void deleteSelectedVideos()
    {
       Iterator<VideoItem> iterator =  mSelectedVideos.iterator();
        while(iterator.hasNext()){
            VideoItem item = (VideoItem) iterator.next();
            Log.i("drj",item.path);
            removeItem(item);
            Log.i("drj"," " + deleteImage(item.path));
        }
        clearSelectedVideos();
    }

    public void  clearSelectedVideos(){
        videoPicker.clearSelectedVideos();
    }

    public int getmSelectedVideoNum() {
        return mSelectedVideos.size();
    }

    private boolean deleteImage(String imgPath) {
        int count = getActivity().getContentResolver().delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                MediaStore.Audio.Media.DATA + "= \"" + imgPath+"\"",
                null);

        return count == 1;
    }
    private Activity getActivity(){
        return (Activity) mContext;
    }

}